// pages/manager/addmeal/addmeal.js
import Toast from '@vant/weapp/toast/toast';

let meal_name = ""
let price = "1.00"
let num = "1"
let kind = "1"
let intro = "我是好吃的饭"
let meal_img = ""
let meal_img1 = ""
let meal_img2 = ""
let meal_img3 = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList1: [],
    fileList2: [],
    fileList3: [],
    camera: ['album', 'camera'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  
  // 饭菜的名称
  onChange1(event) {
    // console.log(event)
    var that = this;
    if(event.detail.length == 0) {
      that.setData({
        nameMsg: "饭菜名不能为空"
      })
    }else {
      that.setData({
        nameMsg: "",
      })
      meal_name = event.detail
      console.log(meal_name)
    }
  },

  // 饭菜的价格
  onChange2(event) {
    price = event.detail
  },
  // 饭菜份数
  onChange3(event) {
    num = event.detail
  },
  // 种类的选择
  kindClick(event) {
    kind = event.detail.name
    console.log(kind)
  },
  // 饭菜介绍
  onChange4(event) {
    intro = event.detail.value
  },
  submit:function() {
    this.submit2(false) // 上架中
  },
  save:function() {
    this.submit2(true)  // 下架中（暂时保存）
  },
  // 提交表单
  submit2:function (status) {
    var that = this;
    const { fileList1 = [] } = this.data;
    const { fileList2 = [] } = this.data;
    const { fileList3 = [] } = this.data;
    if(meal_name.length == 0) {
      that.setData({
        nameMsg: "饭菜名不能为空"
      })
      return
    }
    if(fileList1.length == 0) {
      Toast.fail('封面图必须上传');
    }else if(fileList1[0].status=="uploading" || (fileList2.length>0&&fileList2[0].status=="uploading") || (fileList3.length>0&&fileList3[0].status=="uploading")) {
      Toast.fail('图片还未上传成功，请等待');
    } else {
      // console.log(meal_name + '-' + price + '-' + num + '-' + kind + '-' + intro + '-' + meal_img1)
      // console.log(meal_img2)
      // console.log(meal_img3)
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'addmeal',
          meal_name: meal_name,
          meal_num: parseInt(num),
          meal_price: parseFloat(price),
          meal_detail: intro,
          meal_img: meal_img1,
          meal_img2: meal_img2,
          meal_img3: meal_img3,
          meal_status: status,
          meal_type: kind,
          meal_week_count: 0,
          meal_totle_count: 0,
          window: {window_id:wx.getStorageSync('window_id'),window_name:wx.getStorageSync('window_name')},
          canteen: wx.getStorageSync('canteen')
        },
        success(res) {
          console.log("云函数增加数据成功",res)
          wx.redirectTo({
            url: '../manager?status=1',
          })
        },
        fail(res) {
          console.log("函数增加数据失败",res)
        }
      })
    }
  },


  // 图片的上传
  
  deleteCloudSave(fileID) {
    wx.cloud.deleteFile({
      fileList: [fileID],
      success: res => {
        // handle success
        console.log(res.fileList)
      },
      fail: console.error
    })
  },
  // 上传到云开发的存储中
  uploadImage(fileURL, fileID, fileList4) {
    var that = this
    wx.cloud.uploadFile({
      cloudPath:'./饭菜封面图和配图/' + new Date().getTime()+'.png', // 上传至云端的路径
      filePath: fileURL, // 小程序临时文件路径
      success: res => {
        //获取图片的http路径
        that.addImagePath(res.fileID,fileList4)
        that.setData({
          [fileID]: res.fileID
        })
      },
      fail: console.error
    })
  },
  // 获取图片上传后的url路径
  addImagePath(fileId,fileList4) {
    var that = this
    wx.cloud.getTempFileURL({
      fileList: [{
        fileID:fileId
      }],
      success: res => {
        console.log(res)
        meal_img = res.fileList[0].tempFileURL
        let fileList  = [];
        if(fileList4 == "fileList1") {
          fileList = that.data.fileList1
          meal_img1 = meal_img
        } else if (fileList4 == "fileList2") {
          fileList = that.data.fileList2
          meal_img2 = meal_img
        } else if (fileList4 == "fileList3") {
          fileList = that.data.fileList3
          meal_img3 = meal_img
        }
        
        fileList[0].url= meal_img
        fileList[0].status= 'done'
        that.setData({ 
          [fileList4] : fileList
        });
      },
      fail: console.error
    })
  }, 
  // 点击上传图片后的状态
  afterRead1(event) {
    console.log(event)
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const { fileList1 = [] } = this.data;
    fileList1.push({})
    fileList1[0].status='uploading'
    this.setData({ fileList1 });
    this.uploadImage(file.url,"fileID1","fileList1");
  },
  // 点击上传图片后的状态
  afterRead2(event) {
    console.log(event)
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const { fileList2 = [] } = this.data;
    fileList2.push({})
    fileList2[0].status='uploading'
    this.setData({ fileList2 });
    this.uploadImage(file.url,"fileID2","fileList2");
  },
 // 点击上传图片后的状态
 afterRead3(event) {
  console.log(event)
  const { file } = event.detail;
  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
  const { fileList3 = [] } = this.data;
  fileList3.push({})
  fileList3[0].status='uploading'
  this.setData({ fileList3 });
  this.uploadImage(file.url,"fileID3","fileList3");
},

  // 点击预览的x号
  deleteImg1(event) {
    // 页面删除
    const delIndex = event.detail.index
    const { fileList1 } = this.data
    fileList1.splice(delIndex, 1)
    this.setData({
      fileList1
    })
    meal_img1 = ""
    // 云存储删除
    var fileID1 = this.data.fileID1;
    this.deleteCloudSave(fileID1)
  },
  // 点击预览的x号
  deleteImg2(event) {
    // 页面删除
    const delIndex = event.detail.index
    const { fileList2 } = this.data
    fileList2.splice(delIndex, 1)
    this.setData({
      fileList2
    })
    meal_img2 = ""
    // 云存储删除
    var fileID2 = this.data.fileID2;
    this.deleteCloudSave(fileID2)
  },
  // 点击预览的x号
  deleteImg3(event) {
    // 页面删除
    const delIndex = event.detail.index
    const { fileList3 } = this.data
    fileList3.splice(delIndex, 1)
    this.setData({
      fileList3
    })
    meal_img3 = ""
    // 云存储删除
    var fileID3 = this.data.fileID3;
    this.deleteCloudSave(fileID3)
  },

})